home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / dev / c / agl103p.lha / src / basename / basename.c
C/C++ Source or Header  |  1994-11-18  |  611b  |  40 lines

  1. /* equivalent of Unix's basename */
  2.  
  3. #include<stdlib.h>
  4. #include<string.h>
  5.  
  6. int main(int argc,char **argv)
  7.     {
  8.     char *pointer,base[100];
  9.     short baselength,taillength;
  10.  
  11.     if(argc<2 || argc>3)
  12.         {
  13.         printf("Usage: basename <filename> [tail]\n");
  14.         exit(1);
  15.         }
  16.  
  17.     pointer=strrchr(argv[1],'/');
  18.     if(pointer==NULL)
  19.         pointer=argv[1];
  20.     else
  21.         pointer++;
  22.  
  23.     strcpy(base,pointer);
  24.  
  25.     if(argc==3)
  26.         {
  27.         baselength=strlen(base);
  28.         taillength=strlen(argv[2]);
  29.  
  30.         pointer=base+baselength-taillength;
  31.  
  32.         if( pointer>=base && !strcmp(pointer,argv[2]) )
  33.             base[baselength-taillength]=0;
  34.         }
  35.  
  36.     printf("%s\n",base);
  37.  
  38.     return 0;
  39.     }
  40.